import { ref, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { getCategoryApi } from '@/apis/category'

export function useCategory() {
    const categoryData = ref({})
    const route = useRoute()
    // watch监听路由对象的'params.id'属性变化
    watch(() => route.params.id, (newId) => {
        getCategory(newId)
    })

    const getCategory = async (id) => {
        const res = await getCategoryApi(id)
        categoryData.value = res.data.result
    }
    onMounted(() => getCategory(route.params.id))

    return {
        categoryData
    }
}